package com.tanxin.bill.common.data;

import java.util.Date;

import io.objectbox.annotation.Convert;
import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.converter.PropertyConverter;
import io.objectbox.relation.ToOne;

@Entity
public class BillData {
    @Id
    private long id;
    private double money;
    private Date startData;
    private Date handleData;
    @Convert(converter = TypeConverter.class, dbType = Integer.class)
    private Type type;
    public ToOne<CustomerData> customerDataToOne;

    public enum Type{
        /**
         * 借款
         * */
        LOAN(0),
        /**
         * 还钱
         * */
        REPAY(1);

        final int flag;
        Type(int flag){
            this.flag = flag;
        }
    }

    public static class TypeConverter implements PropertyConverter<Type, Integer> {
        @Override
        public Type convertToEntityProperty(Integer databaseValue) {
            if (databaseValue == null) {
                return null;
            }
            for (Type type : Type.values()) {
                if (type.flag == databaseValue) {
                    return type;
                }
            }
            return Type.LOAN;
        }

        @Override
        public Integer convertToDatabaseValue(Type entityProperty) {
            return entityProperty == null ? null : entityProperty.flag;
        }
    }

    public Date getStartData() {
        return startData;
    }

    public void setStartData(Date startData) {
        this.startData = startData;
    }

    public Date getHandleData() {
        return handleData;
    }

    public void setHandleData(Date handleData) {
        this.handleData = handleData;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public ToOne<CustomerData> getCustomerDataToOne() {
        return customerDataToOne;
    }

    public void setCustomerDataToOne(ToOne<CustomerData> customerDataToOne) {
        this.customerDataToOne = customerDataToOne;
    }
}
